/**
 * @author LYJ
 * Created by Administrator on 2018/2/27 0027.
 */
function AdminLoginForm(option) {
    BootstrapValidateBaseForm.call(this,option.$form,option.$error);
    var This = this;

    this.init = {
        start: function () {
            This.$error.hintModal();
            if (This.type == null || This.type == "login") {
                this.login();
            }

            This.element("submit").bind("click",function () {
                This.run();
            });
        },
        login: function () {
            This.$form.bootstrapValidator({
                message: '填写值无效',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                live:"enabled",
                fields: {
                    username: {
                        message: '用户名无效',
                        validators: {
                            notEmpty: {
                                message: '用户名不能为空'
                            },
                            remote: {
                                type:"post",
                                url:"login/validatorUsername",
                                message:"该用户不存在"
                            }
                        }
                    },
                    password: {
                        validators: {
                            notEmpty: {
                                message: '密码不能为空'
                            },
                            remote:{
                                type:"post",
                                url:"login/validatorPassword",
                                data:function(validator) {
                                    return{
                                        username:This.param("username").val()
                                    }
                                },
                                message:"密码错误"
                            }
                        }
                    }
                }
            });
        }
    };

    this.ajaxForm = {
        start: function () {
            This.validate();
            setTimeout(function () {
                if(!This.$form.data("bootstrapValidator").isValid()){
                    return;
                }

                if (This.type == null || This.type == "login") {
                    This.ajaxForm.login();
                }
            },500);
        },
        login: function () {
            This.element("submit").attr("disabled",true);
            This.$form.ajaxSubmit({
                type:"post",
                url:"login",
                dataType:"json",
                success: function (data) {
                    if(data["recode"] == 0){
                        window.location.href = "foods";
                    }else{
                        This.element("submit").attr("disabled",false);
                        This.$error.hintModal().showError(data["error"],1000);
                    }
                }
            });
        }
    };
}(function () {
    AdminLoginForm.prototype = BootstrapValidateBaseForm.prototype;
    AdminLoginForm.prototype.constructor = AdminLoginForm;
})();